<?php

class Chuc_nangsController extends BaseController {

    /**
     * Chuc_nang Repository
     *
     * @var Chuc_nang
     */
    protected $chuc_nang;

    public function __construct(Chuc_nang $chuc_nang) {
        $this->chuc_nang = $chuc_nang;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        $chuc_nangs = $this->chuc_nang->all();

        return View::make('chuc_nangs.index', compact('chuc_nangs'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create() {
        return View::make('chuc_nangs.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        $input = Input::all();
        $validation = Validator::make($input, Chuc_nang::$rules);

        if ($validation->passes()) {
            $this->chuc_nang->create($input);

            return Redirect::route('functions.index');
        }

        return Redirect::route('functions.create')
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
        $chuc_nang = $this->chuc_nang->findOrFail($id);

        return View::make('chuc_nangs.show', compact('chuc_nang'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id) {
        $chuc_nang = $this->chuc_nang->find($id);

        if (is_null($chuc_nang)) {
            return Redirect::route('functions.index');
        }

        return View::make('chuc_nangs.edit', compact('chuc_nang'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id) {
        $input = array_except(Input::all(), '_method');
        $validation = Validator::make($input, Chuc_nang::$rules);

        if ($validation->passes()) {
            $chuc_nang = $this->chuc_nang->find($id);
            $chuc_nang->update($input);

            return Redirect::route('functions.show', $id);
        }

        return Redirect::route('functions.edit', $id)
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
        $this->chuc_nang->find($id)->delete();

        return Redirect::route('functions.index');
    }

}
